<template>
  <div id="statistics">
    <div class="condition">
      <span>发震时间</span>
      <RadioGroup v-model:value="oritime" @change="handleTime()">
        <Radio
          :label="item.label"
          :value="item.value"
          v-for="(item, index) in options"
          :key="index + 'date'"
          >{{ item.label }}</Radio
        >
      </RadioGroup>
    </div>
    <div class="header">
      <div class="left">
        <earthquake-level :type="'1'" :time="time" />
        <span class="border"></span>
        <earthquakeTrend :type="'1'" :time="time" />
      </div>
      <span class="center"></span>
      <div class="right">
        <earthquake-level :type="'2'" :time="time" />
        <span class="border"></span>
        <earthquakeTrend :type="'2'" :time="time" />
      </div>
    </div>
    <div class="footer">
      <earthquake-area :time="time" />
    </div>
  </div>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import { Radio, RadioGroup } from 'ant-design-vue';
import dayjs from 'dayjs';
import earthquakeArea from './component/area.vue';
import earthquakeLevel from './component/level.vue';
import earthquakeTrend from './component/trend.vue';

const options = [
  {
    label: '近七天',
    value: 'lastWeekRange',
  },
  {
    label: '上个月',
    value: 'lastMonthRange',
  },
  {
    label: '近三个月',
    value: 'last3MonthsRange',
  },
  {
    label: '近半年',
    value: 'last6MonthsRange',
  },
  {
    label: '近一年',
    value: 'lastYearRange',
  },
];
const getDateRange = () => {
  // 获取当前时间
  const today = dayjs();
  const lastWeekStart = today.subtract(7, 'days');
  // 上一个月的起止时间
  const lastMonthStart = today.clone().subtract(1, 'month').startOf('month');
  const lastMonthEnd = today.clone().subtract(1, 'month').endOf('month');
  // 近三月的起止时间
  const last3MonthsStart = today.clone().subtract(3, 'month').startOf('month');
  const last3MonthsEnd = today.clone().subtract(1, 'month').endOf('month');
  // 近半年的起止时间
  const last6MonthsStart = today.clone().subtract(6, 'month').startOf('month');
  const last6MonthsEnd = today.clone().subtract(1, 'month').endOf('month');
  // 近一年的起止时间
  const lastYearStart = today.clone().subtract(12, 'month').startOf('month');
  const lastYearEnd = today.clone().subtract(1, 'month').endOf('month');
  // 格式化日期字符串
  const format = 'x';
  const lastWeekRange = [lastWeekStart.valueOf(), dayjs().valueOf()];
  const lastMonthRange = [lastMonthStart.valueOf(), lastMonthEnd.valueOf()];
  const last3MonthsRange = [
    last3MonthsStart.valueOf(),
    last3MonthsEnd.valueOf(),
  ];
  const last6MonthsRange = [
    last6MonthsStart.valueOf(),
    last6MonthsEnd.valueOf(),
  ];
  const lastYearRange = [lastYearStart.valueOf(), lastYearEnd.valueOf()];
  // 返回日期范围
  return {
    lastWeekRange,
    lastMonthRange,
    last3MonthsRange,
    last6MonthsRange,
    lastYearRange,
  };
};

const oritime = ref('lastWeekRange');
const time = ref({
  startTime: getDateRange()[oritime.value][0],
  endTime: getDateRange()[oritime.value][1],
  type: '1',
});

const handleTime = () => {
  time.value = {
    startTime: getDateRange()[oritime.value][0],
    endTime: getDateRange()[oritime.value][1],
  };
  if (oritime.value == 'last6MonthsRange' || oritime.value == 'lastYearRange') {
    time.value.type = '2';
  } else {
    time.value.type = '1';
  }
};
</script>

<style lang="scss" scoped>
#statistics {
  width: 100%;
  height: 100%;
  padding: 0 20px;
  overflow-y: auto;
  .condition {
    padding-top: 10px;
    > span {
      margin-right: 15px;
      font-size: 14px;
      font-weight: 600;
      color: #fff;
    }
  }

  .header {
    display: flex;
    margin-top: 20px;

    > div {
      display: flex;
      flex-direction: column;
      align-items: center;
      flex: 1;
      padding: 10px 20px;
      border-radius: 12px;
      background: rgba(0, 0, 0, 0.24);
      // box-shadow: 1px 0px 5px 5px rgba(228, 230, 225, .6);
      // -webkit-box-shadow: 1px 0px 5px 5px rgba(228, 230, 225, .6);

      .border {
        height: 1px;
        width: 95%;
        margin-bottom: 10px;
        background-color: rgba(228, 230, 225, 0.8);
      }
    }

    .center {
      width: 20px;
      height: 80%;
    }
  }

  .footer {
    height: 28vh;
    width: 100%;
    margin-top: 20px;
    padding: 10px 20px;
    background: rgba(0, 0, 0, 0.24);
    border-radius: 12px;
    // box-shadow: 1px 0px 10px 5px rgba(228, 230, 225, .6);
    // -webkit-box-shadow: 1px 0px 5px 5px rgba(228, 230, 225, .6);
  }
}
</style>
